Ext.namespace('Divo');
Ext.namespace('Divo.app');
Divo.app.checkGrid = Ext.extend(Ext.grid.EditorGridPanel, {
      initComponent : function() {
        var me = this;
        var typeCode;
        var readerRecord = Ext.data.Record.create([{
              name : "id",
              type : "float"
            }, {
              name : "readyCodeId",
              type : "float"
            }, {
              name : "number",
              type : "string"
            }, {
              name : "time",
              type : "string"
            }, {
              name : "type",
              type : "String"
            }, {
              name : "materialName",
              type : "string"
            }, {
              name : "readyCodeNumber",
              type : "string"
            }, {
              name : "managerCode",
              type : "string"
            }, {
              name : "factoryCode",
              type : "string"
            }, {
              name : "hNumber",
              type : "string"
            }, {
              name : "name",
              type : "string"
            }, {
              name : "readySendWho",
              type : "string"
            }, {
              name : "tName",
              type : "string"
            }, {
              name : "readySendTime",
              type : "string"
            }, {
              name : "regTime",
              type : "string"
            }, {
              name : "regWho",
              type : "string"
            }]);

        var ds = new Ext.data.Store({
              proxy : new Ext.data.HttpProxy({
                    url : 'reg_account/query_reg_account'
                  }),
              reader : new Ext.data.JsonReader({
                    root : 'rows',
                    totalProperty : 'totalCount',
                    id : 'id'
                  }, readerRecord)
            });
        var cm = new Ext.grid.ColumnModel({
              columns : [{
                    header : '',
                    dataIndex : 'id',
                    hidden : true
                  }, {
                    header : '',
                    dataIndex : 'readyCodeId',
                    hidden : true
                  }, {
                    header : '<font color="blue">发票号</font>',
                    dataIndex : 'number',
                    editor : new app.base.NumberField({
                          name : "number",
                          dataIndex : "number",
                          allowBlank : false,
                          width : 100,
                          insert_allowed : true,
                          update_allowed : true
                        })
                  }, {
                    header : '<font color="blue">发票日期</font>',
                    dataIndex : 'time',
                    xtype : 'datecolumn',
                    format : 'Y.m.d',
                    width : 150,
                    editor : new app.base.DateField({
                          name : "time",
                          dataIndex : "time",
                          format : 'Y.m.d',
                          width : 150,
                          editable : true,
                          insert_allowed : true,
                          update_allowed : true
                        })
                  }, {
                    header : '<font color="blue">结算方式</font>',
                    dataIndex : 'type',
                    width : 150,
                    editor : new app.base.ComboBox({
                          name : 'type',
                          store : Divo.com.Lib.getAccountTypeDs(),
                          displayField : 'name',
                          dataIndex : 'type',
                          mode : 'remote',
                          typeAhead : false,
                          forceSelection : true,
                          loadingText : '正在搜索...',
                          listWidth : 300,
                          width : 150,
                          minChars : 0,
                          allowBlank : false,
                          insert_allowed : true,
                          update_allowed : true,
                          onSelect : function(record) {
                            var data = record.data;
                            this.setValue(data.name);
                            this.setHideValue(data.code);
                            this.onTriggerClick();
                          }
                        })
                  }, {
                    header : '厂家耗材名',
                    dataIndex : 'materialName',
                    width : 100
                  }, {
                    header : '备码次数',
                    dataIndex : 'readyCodeNumber',
                    width : 100
                  }, {
                    header : '管理码',
                    dataIndex : 'managerCode',
                    width : 100
                  }, {
                    header : '厂家码',
                    dataIndex : 'factoryCode',
                    width : 100
                  }, {
                    header : '病人住院号',
                    dataIndex : 'hNumber',
                    width : 100
                  }, {
                    header : '病人姓名',
                    dataIndex : 'name',
                    width : 100
                  }, {
                    header : '配发护士',
                    dataIndex : 'readySendWho',
                    width : 100
                  }, {
                    header : '科室名称',
                    dataIndex : 'tName',
                    width : 100
                  }, {
                    header : '配发时间',
                    dataIndex : 'readySendTime',
                    width : 100
                  }, {
                    header : '操作时间',
                    dataIndex : 'regTime',
                    width : 100
                  }, {
                    header : '操作人',
                    dataIndex : 'regWho',
                    width : 100
                  }]
            });
        Ext.apply(this, {
              store : ds,
              cm : cm,
              loadMask : true,
              clicksToEdit : 1
            });
        Divo.app.checkGrid.superclass.initComponent.apply(this, arguments);
      },
      onRender : function() {
        Divo.app.checkGrid.superclass.onRender.apply(this, arguments);
      },
      reloadRecords : function() {
        this.getStore().reload();
      }
    });

Divo.app.RegAccountList = function() {
  /* ----------------------- private属性 ----------------------- */
  var gridEl = 'RegAccount-grid';
  var grid, form, selModel, toolbar, win;
  var data, sId;
  var fldTime, fldCode;
  /* ----------------------- private方法 ----------------------- */
  // 创建列表
  function createGrid() {
    var queryFlds = new Array();
    var searchFlds = new Ext.util.MixedCollection();
    queryFlds.push('name');
    var fldName = new Ext.form.TextField({
          name : 'name',
          id : gridEl + '-name',
          fieldLabel : '名称',
          allowBlank : true,
          width : 100

        });
    searchFlds.add('name', fldName);

    grid = new Divo.app.checkGrid({
          remoteSort : true,
          gridId : gridEl + "check",
          queryFlds : queryFlds,
          searchFlds : searchFlds,
          hasBbar : false,
          jsonId : 'id',
          recordPk : ["id"]
        });
  }

  // 刷新列表
  function reloadRecords() {
    grid.getStore().reload();
  }

  // 创建layout
  function createLayout() {
    toolbar = new Ext.Toolbar({
          items : [{
                icon : "resources/img/g_rec_new.png",
                text : "入库入账",
                id : "tlb_REG",
                handler : regAccount
              }, {
                icon : "resources/img/g_rec_refresh.gif",
                text : "刷新",
                id : "tlb_REFRESH",
                handler : reloadRecords
              }]
        });
    var viewport = new Ext.Viewport({
          layout : 'border',
          items : [{
                layout : 'border',
                region : 'center',
                border : false,
                items : [{
                      layout : 'fit',
                      region : 'north',
                      border : false,
                      items : [toolbar],
                      height : 25
                    }, {
                      layout : 'fit',
                      region : 'center',
                      border : false,
                      items : [grid]
                    }]
              }]
        });
    viewport.doLayout();
  }

  function regAccount() {
    // TODO
    // 判断所有已经填写的记录，未填写的不提交
    var gridStore = grid.getStore();
    var strs;
    for (var i = 0; i < gridStore.getCount(); i++) {
      var record = gridStore.getAt(i);
      var number = record.get("number");
      var timeO = record.data.time;
      var time = timeO.format("Y.m.d");
      var type = record.get("type");
      var readyCodeId = record.get("readyCodeId");
      if ((number != null && number != "") && (time != null && time != "")
          && (type != null && type != "")) {
        if (strs == null || strs == "") {
          strs = number + "," + time + "," + type + "," + readyCodeId;
        } else {
          strs = strs + "-" + number + "," + time + "," + type + ","
              + readyCodeId;
        }
      }
    }

    if (null != strs && strs != "") {
      Ext.Ajax.request({
            params : {
              strs : strs
            },
            url : "reg_account/save_reg",
            callback : function(o, s, r) {
              var respText = Ext.decode(r.responseText);
              if (respText.success) {
                grid.getStore().load();
              } else {
                if (respText.message.length > 0) {
                  this.startEditing(cm.row, cm.column);
                  Ext.Msg.alert('错误', respText.message);
                }
              }
            }
          });
    }

  }

  // 切换过滤查询工具条状态
  function toggleFilterBar() {
    grid.toggleFilterBar();
  }

  function onLoad() {
    var store = grid.getStore();
    if (store.getCount() > 0)
      grid.startEditing(0, 2);
  }

  /* ----------------------- public方法 ----------------------- */
  return {
    /**
     * 初始化
     */
    init : function() {
      createGrid();
      createLayout();
      grid.getStore().load();
      grid.getStore().on("load", onLoad);
    }
  }
}();

Ext.onReady(Divo.app.RegAccountList.init, Divo.app.RegAccountList, true);